import os
import qianfan
from ChatConfig import config
#【推荐】使用安全认证AK/SK鉴权，通过环境变量初始化认证信息
# 替换下列示例中参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = config.QIANFAN_ACCESS_KEY
os.environ["QIANFAN_SECRET_KEY"] = config.QIANFAN_SECRET_KEY
chat_comp = qianfan.ChatCompletion()

def chat(message)->str:
    try:
        # 指定特定模型
        resp = chat_comp.do(model="Yi-34B-Chat", messages=[{
            "role": "user",
            "content": '请简短回答我,尽量不要超过50个字,问题:'+message
        }])
        return resp["body"]['result']
    except qianfan.errors.APIError:
        return "服务器繁忙，请稍后再试!"
if __name__ == '__main__':
     print(chat('你好'))